package com.yy.jhl.ui.main.activity;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import com.yy.common.baseapp.AppManager;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonwidget.StatusBarCompat;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.ui.manager.activity.MenuOneActivity;
import com.yy.jhl.ui.manager.activity.MenuThreeActivity;
import com.yy.jhl.ui.manager.activity.MenuTwoJGActivity;
import com.yy.jhl.ui.manager.activity.MenuTwoRWActivity;
import com.yy.jhl.ui.manager.activity.StatisticsWebViewActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Ellis.Li on 2017/8/28.
 */

public class MainActivity extends TabActivity {


    @BindView(android.R.id.tabs)
    TabWidget tabs;
    @BindView(android.R.id.tabcontent)
    FrameLayout tabcontent;
    @BindView(R.id.first_radio_img)
    ImageView firstRadioImg;
    @BindView(R.id.first_radio_text)
    TextView firstRadioText;
    @BindView(R.id.first_radio_btn)
    LinearLayout firstRadioBtn;
    @BindView(R.id.second_radio_img)
    ImageView secondRadioImg;
    @BindView(R.id.second_radio_text)
    TextView secondRadioText;
    @BindView(R.id.second_radio_btn)
    LinearLayout secondRadioBtn;
    @BindView(R.id.three_radio_img)
    ImageView threeRadioImg;
    @BindView(R.id.three_radio_text)
    TextView threeRadioText;
    @BindView(R.id.three_radio_btn)
    LinearLayout threeRadioBtn;
    @BindView(R.id.statistics_radio_img)
    ImageView statisticsRadioImg;
    @BindView(R.id.statistics_radio_text)
    TextView statisticsRadioText;
    @BindView(R.id.statistics_radio_btn)
    LinearLayout statisticsRadioBtn;
    @BindView(R.id.menu_parent)
    LinearLayout menuParent;
    @BindView(R.id.main_tab_group)
    LinearLayout mainTabGroup;
    @Nullable
    TabHost tabhost;
    @Nullable
    TabWidget tabWidget;
    Context mContext;
    //记录用户首次点击返回键的时间
    private long firstTime=0;
    public static void startAction(Context mContext) {
        Intent intent = new Intent(mContext, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
        mContext.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//        }
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 把actvity放到application栈中管理
        AppManager.getAppManager().addActivity(this);
        // 无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // 默认着色状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            StatusBarCompat.setStatusBarColor(this, getResources().getColor(com.yy.common.R.color.main_blue));
        }
        setContentView(R.layout.act_main);
        ButterKnife.bind(this);
        mContext = this;
        loadIntent();
        setCurrentTabChecked(true, false, false,false);
    }
    private void loadIntent() {
        tabhost = this.getTabHost();
        tabhost.addTab(tabhost.newTabSpec(getString(R.string.first_tab_host))
                .setIndicator(getString(R.string.first_tab_host))
                .setContent(new Intent(mContext, MenuOneActivity.class)));
        //供应商
        if (SPUtils.getSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE) == 1) {
            statisticsRadioBtn.setVisibility(View.GONE);
            secondRadioText.setText("任务订单");
            tabhost.addTab(tabhost.newTabSpec(getString(R.string.second_tab_host))
                    .setIndicator(getString(R.string.second_tab_host))
                    .setContent(new Intent(mContext, MenuTwoRWActivity.class)));
        } else { //采购商
            // TODO: 2018/3/17
            statisticsRadioBtn.setVisibility(View.GONE);
            secondRadioText.setText("监管订单");
            tabhost.addTab(tabhost.newTabSpec(getString(R.string.second_tab_host))
                    .setIndicator(getString(R.string.second_tab_host))
                    .setContent(new Intent(mContext, MenuTwoJGActivity.class)));
            tabhost.addTab(tabhost.newTabSpec(getString(R.string.statistics_tab_host))
                    .setIndicator(getString(R.string.statistics_tab_host))
                    .setContent(new Intent(mContext, StatisticsWebViewActivity.class)));
        }
        tabhost.addTab(tabhost.newTabSpec(getString(R.string.three_tab_host))
                .setIndicator(getString(R.string.three_tab_host))
                .setContent(new Intent(mContext, MenuThreeActivity.class)));

    }
    private void setCurrentTabChecked(boolean oneChk, boolean twoChk, boolean threeChk,boolean statistics) {

        if (oneChk) {
            StatusBarCompat.setStatusBarColor(this, getResources().getColor(com.yy.common.R.color.main_blue));
            tabhost.setCurrentTabByTag(getString(R.string.first_tab_host));
            firstRadioImg.setImageResource(R.mipmap.project_icon_press);
            secondRadioImg.setImageResource(R.mipmap.order_icon_normal);
            threeRadioImg.setImageResource(R.mipmap.my_icon_normal);
            statisticsRadioImg.setImageResource(R.mipmap.statistics_icon_normal);
            statisticsRadioText.setTextColor(getResources().getColor(R.color.menu_normal_color));
            firstRadioText.setTextColor(getResources().getColor(R.color.menu_press_color));
            secondRadioText.setTextColor(getResources().getColor(R.color.menu_normal_color));
            threeRadioText.setTextColor(getResources().getColor(R.color.menu_normal_color));
        }
        if (twoChk) {
            StatusBarCompat.setStatusBarColor(this, getResources().getColor(com.yy.common.R.color.main_blue));
            tabhost.setCurrentTabByTag(getString(R.string.second_tab_host));
            firstRadioImg.setImageResource(R.mipmap.project_icon_normal);
            secondRadioImg.setImageResource(R.mipmap.order_icon_press);
            threeRadioImg.setImageResource(R.mipmap.my_icon_normal);
            firstRadioText.setTextColor(getResources().getColor(R.color.menu_normal_color));
            secondRadioText.setTextColor(getResources().getColor(R.color.menu_press_color));
            threeRadioText.setTextColor(getResources().getColor(R.color.menu_normal_color));
            statisticsRadioImg.setImageResource(R.mipmap.statistics_icon_normal);
            statisticsRadioText.setTextColor(getResources().getColor(R.color.menu_normal_color));
        }
        if (threeChk) {
            StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.deep_green_color));
            tabhost.setCurrentTabByTag(getString(R.string.three_tab_host));
            firstRadioImg.setImageResource(R.mipmap.project_icon_normal);
            secondRadioImg.setImageResource(R.mipmap.order_icon_normal);
            threeRadioImg.setImageResource(R.mipmap.my_icon_press);
            firstRadioText.setTextColor(getResources().getColor(R.color.menu_normal_color));
            secondRadioText.setTextColor(getResources().getColor(R.color.menu_normal_color));
            threeRadioText.setTextColor(getResources().getColor(R.color.menu_press_color));
            statisticsRadioImg.setImageResource(R.mipmap.statistics_icon_normal);
            statisticsRadioText.setTextColor(getResources().getColor(R.color.menu_normal_color));
        }
        if (statistics) {
            StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.deep_green_color));
            tabhost.setCurrentTabByTag(getString(R.string.statistics_tab_host));
            firstRadioImg.setImageResource(R.mipmap.project_icon_normal);
            secondRadioImg.setImageResource(R.mipmap.order_icon_normal);
            threeRadioImg.setImageResource(R.mipmap.my_icon_normal);
            firstRadioText.setTextColor(getResources().getColor(R.color.menu_normal_color));
            secondRadioText.setTextColor(getResources().getColor(R.color.menu_normal_color));
            threeRadioText.setTextColor(getResources().getColor(R.color.menu_normal_color));
            statisticsRadioImg.setImageResource(R.mipmap.statistics_icon_press);
            statisticsRadioText.setTextColor(getResources().getColor(R.color.menu_press_color));
        }

    }
    @OnClick({R.id.first_radio_btn, R.id.second_radio_btn, R.id.three_radio_btn, R.id.statistics_radio_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.first_radio_btn:
                setCurrentTabChecked(true,false,false,false);
                break;
            case R.id.second_radio_btn:
                setCurrentTabChecked(false,true,false,false);
                break;
            case R.id.three_radio_btn:
                setCurrentTabChecked(false,false,true,false);
                break;
            case R.id.statistics_radio_btn:
                setCurrentTabChecked(false,false,false,true);
                break;
        }
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
                &&event.getAction() == KeyEvent.ACTION_DOWN
                && event.getRepeatCount() == 0){
            if (System.currentTimeMillis()-firstTime>2000){
                Toast.makeText(mContext,"再按一次退出程序",Toast.LENGTH_SHORT).show();
                firstTime=System.currentTimeMillis();
            }else{
                finish();
                System.exit(0);
            }
            return false;
        }else{
            return super.dispatchKeyEvent(event);
        }
    }
}
